package org.bluedream.core.utils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.bluedream.core.module.sys.entity.Organization;
import org.bluedream.core.module.sys.service.OrganizationService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @ClassName OrgUtil
 * @Description TODO
 * @Author Administrator
 * @Data 2021/12/2 15:15
 * @Version
 */
public class OrgUtil {
    private static Logger logger = LoggerFactory.getLogger(OrgUtil.class);

    public static boolean isRootNode(String id){
        try {
            OrganizationService organizationService = SpringUtil.getBean(OrganizationService.class);
            Organization o = organizationService.getById(id);
            if (o.getParentCodes().equals("0,")){
                return true;
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
        }
        return false;
    }

    public static Organization getOrganizationById(String id){
        try {
            OrganizationService organizationService = SpringUtil.getBean(OrganizationService.class);
            return organizationService.getById(id);
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
        }
        return null;
    }

    public static String getRootNodeId(){
        try {
            Organization o = getRootNode();
            return o.getId();
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
        }
        return null;
    }

    public static Organization getRootNode(){
        try {
            QueryWrapper<Organization> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("parent_codes;eq" , "0,");
            queryWrapper.eq("parent_code" , "0");
            queryWrapper.eq("code" , "0");

            OrganizationService organizationService = SpringUtil.getBean(OrganizationService.class);
            Organization o = organizationService.getOne(queryWrapper);
            return o;
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
        }
        return null;
    }
}